/*************************************************************************
	> File Name: defines.h
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年01月17日 星期三 15时36分49秒
 ************************************************************************/
#ifndef __GLOBAL_DEFINES__
#define __GLOBAL_DEFINES__

#include "macro.h"
#include <string>
#include <stdio.h>
#include <string.h>

typedef char              S8;
typedef unsigned char     U8;
typedef short             S16;
typedef unsigned short    U16;
typedef int               S32;
typedef unsigned int      U32;
typedef __uint64_t        U64;
typedef __int64_t         S64;
typedef float             F32;
typedef double            F64;

#ifdef __cplusplus
typedef bool              BOOL;
#define TRUE              true
#define FALSE             false
#else
typedef unsigned char     BOOL;
#define TRUE              1
#define FALSE             0
#endif

#define floatcmp(f,d) ((f-d)<0.000001 && (f-d)>-0.000001)

typedef void*            HANDLE;

typedef enum
{
    TYPE_NONE = 0,
    TYPE_U8,
    TYPE_S8,
    TYPE_U16,
    TYPE_S16,
    TYPE_U32,
    TYPE_S32,
    TYPE_U64,
    TYPE_S64,
    TYPE_F32,
    TYPE_F64,
    TYPE_STR,
    TYPE_POINTER,
    TYPE_CMD
}TYPE_DEFINE;

typedef struct
{
    TYPE_DEFINE type;
    union{
        S8   s8;
        U8   u8;
        S16  s16;
        U16  u16;
        S32  s32;
        U32  u32;
        S64  s64;
        U64  u64;
        F32  f32;
        F64  f64;
        S8   str[8];
    }data;
}UnionData;

//环境变量
#define CONFIG_DIR_ENV    "DAS_CONFIG_DIR"
#ifdef HTNICE_K4
#define SYSVER            "C4"
#define MAX_FILE_SIZE     4096
#elif defined(HTNICE_OLDK4)
#define SYSVER            "C4-M0"
#define MAX_FILE_SIZE     2048
#elif defined(HTNICE_K2)
#define SYSVER            "C2"
#define MAX_FILE_SIZE     2048
#else
#define SYSVER            "Gnu/Linux"
#define MAX_FILE_SIZE     4096
#endif

#define NETWAY_PORT       9797   //网关远程端口
#define USER_PORT         9292   //维护软件端口
#define ACQ_PORT          4750   //采集程序端口
#define ASSIST_PORT       4751   //采集辅助程序端口
#define MODBUS_PORT       502

#define MAX_ID_LEN              20
#define MAX_TYPE_LEN            20
#define MAX_VAR_LEN             32
#define MAX_CMD_LEN             256
#define MAX_COND_LEN            256

#define MAX_NAME_LEN            128
#define MAX_SECRETKEY_LEN       128

#define MAX_RECV_SIZE        512
#define MAX_NETADDR_LEN      128
#define MAX_SERIALADDR_LEN   10
#define MAX_REQUEST_SIZE     4096

#include "devid.h"

/*
 * socket address
 */
typedef struct{
    S8  ip_addr[MAX_NETADDR_LEN];/* such as: 192.168.0.1 */
    U16 port;
}socketinfo_t;
typedef socketinfo_t *psocketinfo_t;

/*
 * 串口地址
 */
typedef struct{
    S8         com[MAX_SERIALADDR_LEN];
    S32        baudrate;		           //baudrate
    S8         databit;		               //data bits, 5, 6, 7, 8
    S8	       parity;		               //parity 0: none, 1: odd, 2: even
    S8	       stopbit;		               //stop bits, 1, 2
}serialinfo_t;
typedef serialinfo_t *pserialinfo_t;

/***********************************
 * transport mode
 * 传输方式
 **********************************/
typedef enum{
    TM_TCP=0,
    TM_SEARIAL,
    TM_MAX
}TM_MODE;

/***********************************
 * transport
 * 传输管理
 ***********************************/
class transmgr{
public:
    transmgr(){
        trans_mode = TM_MAX;
        timeout=0;
        sample = 0;
        start = 0;
        memset(&trans_addr,0,sizeof(trans_addr));
    }
    BOOL empty() {
        return trans_mode==TM_MAX;
    }


    //&&
//    (trans_addr.portaddr.baudrate == trans.trans_addr.portaddr.baudrate) &&
//    (trans_addr.portaddr.parity == trans.trans_addr.portaddr.parity) &&
//    (trans_addr.portaddr.databit == trans.trans_addr.portaddr.databit) &&
//    (trans_addr.portaddr.stopbit == trans.trans_addr.portaddr.stopbit)
    BOOL operator==(const transmgr& trans) const{
        if (trans_mode != trans.trans_mode)
            return FALSE;
        if (trans_mode == TM_SEARIAL) {
            if (!strcmp(trans_addr.portaddr.com,
                        trans.trans_addr.portaddr.com))
                return TRUE;
        }
        else if(trans_mode == TM_TCP ){
            if (!strcmp(trans.trans_addr.socketaddr.ip_addr,trans_addr.socketaddr.ip_addr) &&
                    trans.trans_addr.socketaddr.port == trans_addr.socketaddr.port)
                return TRUE;
        }
        return FALSE;
    }
    int   id;
    time_t  start; /* seconds since 1970-1-1  00:00:00 +000 */
    S32     sample; /* sample minutes */
    TM_MODE trans_mode;
    union{
        serialinfo_t   portaddr;
        socketinfo_t   socketaddr;
    }trans_addr;
    S32     timeout;
};
typedef class transmgr transmgr_t;
typedef class transmgr *ptransmgr_t;

/******************************
 *device type
 *
*******************************/
typedef S32 DM_TYPE;

/******************************
 *device info
 *
*******************************/
class deviceinfo {
public:
    S32   id;
    std::string   code;//分项code
    ID_DEV  dev_id;  /* 16进制,具体由解析插件决定 */
    DM_TYPE dev_type;
    std::string name;
    S32     model_id;
    S32     tran_id;
    transmgr_t trans;/* 通讯 */
};
typedef class deviceinfo deviceinfo_t;
typedef class deviceinfo *pdeviceinfo_t;

class energyinfo
{
public:
    S32 id;
    std::string name;
    std::string code;
};

#endif//__GLOBAL_DEFINES__
